package com.wlw.admin.openeye.ui.activity

import android.support.v4.app.Fragment
import android.view.KeyEvent
import com.wlw.admin.openeye.R
import com.wlw.admin.openeye.base.BaseActivity
import com.wlw.admin.openeye.showToast
import com.wlw.admin.openeye.ui.fragment.DiscoveryFragment
import com.wlw.admin.openeye.ui.fragment.HomeFragment
import com.wlw.admin.openeye.ui.fragment.HotFragment
import com.wlw.admin.openeye.ui.fragment.MineFragment
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity() {

    override fun layoutId(): Int = R.layout.activity_main

    private fun switchFragment(fragment: Fragment?) {
        fragment?.let { supportFragmentManager.beginTransaction().replace(R.id.fl_container, it).commit() }
    }

    override fun initView() {
        bnv_tab.setOnNavigationItemSelectedListener {
            return@setOnNavigationItemSelectedListener when (it.itemId) {
                R.id.home -> {
                    switchFragment(HomeFragment.getInstance("每日精选"))
                    true
                }
                R.id.discovery -> {

                    switchFragment(DiscoveryFragment.getInstance("发现"))
                    true
                }
                R.id.hot -> {

                    switchFragment(HotFragment.getInstance("热门"))
                    true
                }
                R.id.mine -> {
                    switchFragment(MineFragment.getInstance())
                    true
                }
                else -> {
                    switchFragment(HomeFragment.getInstance("每日精选"))
                    true
                }
            }
        }

        switchFragment(HomeFragment.getInstance("每日精选"))
    }

    override fun start() {

    }

    override fun initData() {
    }

    private var mExitTime: Long = 0
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis().minus(mExitTime) <= 2000) {
                return super.onKeyDown(keyCode, event)
            } else {
                mExitTime = System.currentTimeMillis()
                showToast("再按一次退出程序")
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

}
